package com.saishi.util;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 阿里连接池
 *
 * @author 阿勋
 */
public class AliPool {
    static DruidDataSource ds;

    //只会运行一次
    static {
        String fname = "druid.properties";
        InputStream file = AliPool.class.getClassLoader().getResourceAsStream(fname);
        Properties prop = new Properties();
        try {
            prop.load(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String driver = prop.getProperty("driver");
        String url = prop.getProperty("url");
        String username = prop.getProperty("username");
        String password = prop.getProperty("password");
        String initialSize = prop.getProperty("initialSize");
        String min = prop.getProperty("minIdle");
        String max = prop.getProperty("maxActive");
        String wait = prop.getProperty("maxWait");

        //配置连接池
        ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ;
        ds.setUsername(username);
        ds.setPassword(password);
        //ds.setInitialSize(Integer.parseInt(initialSize));
        ds.setMinIdle(new Integer(min));
        ds.setMaxActive(new Integer(max));
        ds.setMaxWait(new Long(wait));
    }

    public static DruidDataSource getDs() {
        return ds;
    }

    public static Connection getConn() {
        Connection conn = null;
        try {
            conn = ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}
